package com.wjr.braveheart.adapter.recyclerView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

/**
 * Created by weijinran ，Email 994425089@qq.com，on 2017/10/26.
 * Describe: RecyclerView的通用Adapter
 * RecyclerView.Adapter点击后的数组越界问题 与 getAdapterPosition() 与 getLayoutPosition() 的区别
 * https://www.cnblogs.com/guanxinjing/p/12192114.html
 * PS: Not easy to write code, please indicate.
 */

public abstract class RecyclerCommonAdapter<T> extends RecyclerView.Adapter<ViewHolder> {
    //LayoutId
    private int mLayoutId;
    //通用参数 泛型
    private List<T> mList;
    //实例化View的LayoutInflate
    private LayoutInflater mInflater;
    //多布局支持
    private MultiTypeSupport<T> mMultiTypeSupport;

    public RecyclerCommonAdapter(Context context, List<T> list, int layoutId) {
        this.mInflater = LayoutInflater.from(context);
        this.mList = list;
        this.mLayoutId = layoutId;
    }

    /**
     * 多布局支持
     */
    public RecyclerCommonAdapter(Context context, List<T> list, MultiTypeSupport<T> multiTypeSupport) {
        this(context, list, -1);
        this.mMultiTypeSupport = multiTypeSupport;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //需要多布局
        if (mMultiTypeSupport != null) {
            mLayoutId = viewType;
        }
        /**
         * 实例化View的方式三种
         * View.inflate(mContext, mLayoutId, null);
         * LayoutInflater.from(mContext).inflate(mLayoutId, parent);
         * LayoutInflater.from(mContext).inflate(mLayoutId, parent, false);
         */
        //创建View
        View itemView = mInflater.inflate(mLayoutId, parent, false);
        final ViewHolder viewHolder = new ViewHolder(itemView);
        // 设置点击事件
        if (mItemClickListener != null) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (viewHolder.getAdapterPosition() == RecyclerView.NO_POSITION) {
                        return;
                    }
                    mItemClickListener.onItemClick(v, viewHolder.getAdapterPosition());
                }
            });
        }
        //设置长按事件
        if (mItemLongClickListener != null) {
            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (viewHolder.getAdapterPosition() == RecyclerView.NO_POSITION) {
                        return false;
                    }
                    return mItemLongClickListener.onItemLongClick(v, viewHolder.getAdapterPosition());
                }
            });
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        //绑定ViewHolder
        convert(holder, mList.get(position), position);
    }

    /**
     * 根据当前位置获取不同的viewType
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        if (mMultiTypeSupport != null) {
            return mMultiTypeSupport.getLayoutId(mList.get(position));
        }
        return super.getItemViewType(position);
    }

    /**
     * 绑定数据
     *
     * @param holder
     * @param item     当前数据
     * @param position 当前位置索引
     */
    protected abstract void convert(ViewHolder holder, T item, int position);


    @Override
    public int getItemCount() {
        return mList.size();
    }

    /**
     * 设置点击和长按事件回调
     */
    private OnItemClickListener mItemClickListener;
    private OnItemLongClickListener mItemLongClickListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener listener) {
        this.mItemLongClickListener = listener;
    }
}
